namespace Domotica.Windows.Controls
{
    using Microsoft.VisualBasic;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Input.StylusPlugIns;

    public class InkControl : Label
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        private DynamicRenderer dr;
        private InkPresenter ip;
        private GestureRecognizer reg;
        private StylusPointCollection stylusPoints;

        static InkControl()
        {
            Type owner = typeof(InkControl);
            UIElement.ClipToBoundsProperty.OverrideMetadata(owner, new FrameworkPropertyMetadata(true));
        }

        public InkControl()
        {
            List<WeakReference> list = __ENCList;
            lock (list)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this.stylusPoints = null;
            this.ip = new InkPresenter();
            this.Content = this.ip;
            this.dr = new DynamicRenderer();
            this.ip.AttachVisuals(this.dr.RootVisual, this.dr.DrawingAttributes);
            this.StylusPlugIns.Add(this.dr);
            CustomDynamicRenderer cdr = new CustomDynamicRenderer();
            this.ip.AttachVisuals(cdr.RootVisual, cdr.DrawingAttributes);
            this.StylusPlugIns.Add(cdr);
            this.reg = new GestureRecognizer();
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            if (e.StylusDevice == null)
            {
                this.stylusPoints = new StylusPointCollection();
                Point pt = e.GetPosition(this);
                StylusPoint stylusPoint = new StylusPoint(pt.X, pt.Y);
                this.stylusPoints.Add(stylusPoint);
            }
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            if (e.StylusDevice == null)
            {
                if (this.stylusPoints == null)
                {
                    this.stylusPoints = new StylusPointCollection();
                }
                Point pt = e.GetPosition(this);
                StylusPoint stylusPoint = new StylusPoint(pt.X, pt.Y);
                this.stylusPoints.Add(stylusPoint);
                Stroke stroke = new Stroke(this.stylusPoints);
                stroke.DrawingAttributes = this.dr.DrawingAttributes;
                this.ip.Strokes.Add(stroke);
                this.stylusPoints = null;
                ReadOnlyCollection<GestureRecognitionResult> gestureResults = this.reg.Recognize(this.ip.Strokes);
                if ((gestureResults.Count > 0) && (gestureResults[0].RecognitionConfidence == RecognitionConfidence.Strong))
                {
                    MessageBox.Show(gestureResults[0].ApplicationGesture.ToString());
                }
                this.ip.Strokes.Clear();
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (((e.StylusDevice == null) && (e.LeftButton != MouseButtonState.Released)) && (this.stylusPoints != null))
            {
                Point pt = e.GetPosition(this);
                StylusPoint stylusPoint = new StylusPoint(pt.X, pt.Y);
                this.stylusPoints.Add(stylusPoint);
            }
        }

        protected override void OnStylusDown(StylusDownEventArgs e)
        {
            Stylus.Capture(this);
            this.stylusPoints = new StylusPointCollection();
            StylusPointCollection eventPoints = e.GetStylusPoints(this, this.stylusPoints.Description);
            this.stylusPoints.Add(eventPoints);
        }

        protected override void OnStylusMove(StylusEventArgs e)
        {
            if (this.stylusPoints != null)
            {
                StylusPointCollection newStylusPoints = e.GetStylusPoints(this, this.stylusPoints.Description);
                this.stylusPoints.Add(newStylusPoints);
            }
        }

        protected override void OnStylusUp(StylusEventArgs e)
        {
            if (this.stylusPoints != null)
            {
                StylusPointCollection newStylusPoints = e.GetStylusPoints(this, this.stylusPoints.Description);
                this.stylusPoints.Add(newStylusPoints);
                Stroke stroke = new Stroke(this.stylusPoints);
                this.ip.Strokes.Add(stroke);
                this.stylusPoints = null;
                Stylus.Capture(null);
            }
        }
    }
}

